#!/bin/sh
#
# 提交前检查运行 php unit 测试
#
# current staged diff files
edit_files=`git diff --name-only --staged`
has_test_file=0
for file in ${edit_files}; do
    # check if test file
	# wildcard match or [[ "$file" =~ ^test\/Feature.* ]]; reg match
	if [[ "$file" == tests/Feature* ]] || [[ "$file" == tests/Unit* ]]; then
	    has_test_file=1
	    # test edit unit test
		./vendor/bin/phpunit -v ${file}
		if [ $? -ne 0 ]; then 
			echo "Please check your failed unit test."
			exit 1
		fi
	fi
done

if [ 0 -eq ${has_test_file} ]; then
    echo "no testing file found."
fi

exit 0
